package com.controller.merchant;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.commons.lang3.StringUtils;

import com.dto.merchant.ChildCatalogDTO;
import com.dto.merchant.MasterCatalogDTO;
import com.model.dao.ChildcatalogTable;
import com.model.dao.ChildcatalogTableDAO;
import com.model.dao.MasterCatalogTableDAO;
import com.model.dao.MastercatalogTable;

@ManagedBean(name = "productController")
@SessionScoped
public class ProductController implements Serializable {

	private static final long serialVersionUID = 3793166492917010378L;

	public List<MasterCatalogDTO> findAllMasters() {

		List<MasterCatalogDTO> list = new ArrayList<MasterCatalogDTO>();
		MasterCatalogTableDAO dao = new MasterCatalogTableDAO();
		List<MastercatalogTable> mastersList = dao.findAll();
		for (MastercatalogTable table : mastersList) {
			MasterCatalogDTO dto = new MasterCatalogDTO();
			dto.setItemId(table.getItemId());
			dto.setItemBrand(table.getItemBrand());
			dto.setItemDescription(table.getItemDescription());
			dto.setItemName(table.getItemName());
			if(null!=table.getL1Id()){
			dto.setL1Id(table.getL1Id());
			} if(null!=table.getL2Id()){
			dto.setL2Id(table.getL2Id());
			}if(null!=table.getL3Id()){
			dto.setL3Id(table.getL3Id());
			}
			list.add(dto);
		}
		return list;
	}

	public List<ChildCatalogDTO> findAllItems() {
		List<ChildCatalogDTO> list = new ArrayList<ChildCatalogDTO>();
		ChildcatalogTableDAO dao = new ChildcatalogTableDAO();
		List<ChildcatalogTable> itemsList = dao.findByAll();
		for (ChildcatalogTable table : itemsList) {
			ChildCatalogDTO dto = new ChildCatalogDTO();
			dto.setChildItemId(table.getChildItemId());
			if(StringUtils.isNotBlank(table.getChildItemImagePath())){
			dto.setChildItemImagePath(table.getChildItemImagePath());
			}else{
				dto.setChildItemImagePath("/images/upload/image_not_available.jpg");
			}
			dto.setChildItemPrice(table.getChildItemPrice());
			dto.setChildItemSrp(table.getChildItemSrp());
			dto.setChildItemSize(table.getChildItemSize());
			if (null != table.getInventoryTable()) {
				dto.setItemLocation(table.getInventoryTable().getItemLocation());
				dto.setItemQty(table.getInventoryTable().getItemQty());
			}
			list.add(dto);
		}
		return list;
	}

	public void saveOrUpdate(MasterCatalogDTO dto) throws Exception {
		MasterCatalogTableDAO dao = new MasterCatalogTableDAO();
		MastercatalogTable table = dao.findById(dto.getItemId());
		table.setItemDescription(dto.getItemDescription());
		table.setItemName(dto.getItemName());
		table.setItemBrand(dto.getItemBrand());
		table.setL1Id(dto.getL1Id());
		table.setL2Id(dto.getL2Id());
		table.setL3Id(dto.getL3Id());
		table=dao.merge(table);
	}
	
	public void addProduct(MasterCatalogDTO dto) throws Exception{
		MasterCatalogTableDAO dao=new MasterCatalogTableDAO();
		MastercatalogTable table=new MastercatalogTable();
		table.setItemName(dto.getItemName());
		table.setItemBrand(dto.getItemBrand());
		table.setItemDescription(dto.getItemDescription());
		table.setL1Id(dto.getL1Id());
		table.setL2Id(dto.getL2Id());
		table.setL3Id(dto.getL3Id());
//		table.setItemId(dto.getItemId());
		dao.persist(table);
	}
	
	public void addItem(ChildCatalogDTO dto) throws Exception{
		ChildcatalogTable childcatalogTable = new ChildcatalogTable();
		// TODO WRITE LOGIC for auto increment are add auto increment feature to 
		// column
		childcatalogTable.setMastercatalogTable(new MastercatalogTable(dto.getMasterTableId()));
		//childcatalogTable.setChildItemId(3);
		childcatalogTable.setChildItemImage(dto.getChildItemImage());
		childcatalogTable.setChildItemPrice(dto.getChildItemPrice());
		childcatalogTable.setChildItemSize(dto.getChildItemSize());
		childcatalogTable.setChildItemSrp(dto.getChildItemSrp());
		childcatalogTable.setChildItemImagePath(dto.getChildItemImagePath());
		ChildcatalogTableDAO childcatalogTableDAO = new ChildcatalogTableDAO();
		childcatalogTableDAO.persist(childcatalogTable);
	}
	public static void main(String[] args) {
		ChildcatalogTable childcatalogTable=new ChildcatalogTable();
		childcatalogTable.setMastercatalogTable(new MastercatalogTable(1));
		//childcatalogTable.setChildItemId(3);
		childcatalogTable.setChildItemImage(null);
		childcatalogTable.setChildItemPrice(new BigDecimal(10));
		childcatalogTable.setChildItemSize("100 gms");
		childcatalogTable.setChildItemSrp(new BigDecimal(10));
		childcatalogTable.setChildItemImagePath("");
		ChildcatalogTableDAO childcatalogTableDAO = new ChildcatalogTableDAO();
		try {
			childcatalogTableDAO.persist(childcatalogTable);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}
